from typing import Any
from django.db import models
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.models import CoreModel
from dvadmin.utils.viewset import CustomModelViewSet
from rest_framework import serializers
from .station import Station
from django.conf import settings
import os

# drf专用过滤器模块
from django_filters import rest_framework as filters
from station.utils.image_process import img_proccess_save,img_proccess_delete
from rest_framework.views import APIView



class Guowangbuliang(CoreModel):
    station = models.TextField(verbose_name="station", blank=True, null=True)
    xu_hao = models.TextField(verbose_name="序号", blank=True, null=True)
    wen_ti_fen_lei = models.TextField(verbose_name="问题分类", blank=True, null=True)
    PCR_dan_hao = models.TextField(verbose_name="PCR单号", blank=True, null=True)
    wen_ti_miao_shu = models.TextField(verbose_name="问题描述", blank=True, null=True)
    ju_ti_bu_men = models.TextField(verbose_name="具体部门", blank=True, null=True)
    ze_ren_ren = models.TextField(verbose_name="责任人", blank=True, null=True)
    gen_jin_ren = models.TextField(verbose_name="跟进人", blank=True, null=True)
    gen_ben_yuan_yin_fen_xi = models.TextField(verbose_name="根本原因分析", blank=True, null=True)
    duan_qi_jie_jue_cuo_shi = models.TextField(verbose_name="短期解决措施", blank=True, null=True)
    chang_qi_jie_jue_cuo_shi = models.TextField(verbose_name="长期解决措施", blank=True, null=True)
    jin_zhan_ji_hua = models.TextField(verbose_name="进展计划", blank=True, null=True)
    duan_dian_hao = models.TextField(verbose_name="断点号", blank=True, null=True)
    guan_bi_shi_jian = models.TextField(verbose_name="关闭时间", blank=True, null=True)
    bei_zhu = models.TextField(verbose_name="备注", blank=True, null=True)

    class Meta:
        db_table="station_guowangbuliang"



class GuowangbuliangFilter(filters.FilterSet):
    station = filters.CharFilter(field_name='station', lookup_expr='icontains')
    wen_ti_fen_lei = filters.CharFilter(field_name='wen_ti_fen_lei', lookup_expr='icontains')
    PCR_dan_hao = filters.CharFilter(field_name='PCR_dan_hao', lookup_expr='icontains')
    wen_ti_miao_shu = filters.CharFilter(field_name='wen_ti_miao_shu', lookup_expr='icontains')

    class Meta:
        models = Guowangbuliang
        fields = '__all__'



class GuowangbuliangImage(models.Model):
    file_path = models.ImageField(upload_to='images/guowangbuliang/', verbose_name='图片路径', blank=True, null=True)
    file_name = models.TextField(verbose_name='图片名称', blank=True, null=True)
    guowangbuliang = models.ForeignKey(Guowangbuliang, on_delete=models.CASCADE, verbose_name='guowangbuliang',related_name='images', blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        db_table="station_guowangbuliang_image"
    


from rest_framework import serializers 
class GuowangbuliangImageModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = GuowangbuliangImage
        fields = '__all__'


class GuowangbuliangModelSerializer(CustomModelSerializer):    
    gong_wei_ming_cheng = serializers.SerializerMethodField(read_only=True)
    images = GuowangbuliangImageModelSerializer(many=True, read_only=True)
    # images = serializers.SerializerMethodField()
    # def get_images(self, obj):
        # return 'images-xxxxx'

    def get_gong_wei_ming_cheng(self, obj):
        try:
            # st = Station.objects.filter(code=obj.station)[0]
            st = Station.objects.get(station=obj.station)
            if st:
                return str(st.gong_wei_ming_cheng)
            else:
                return 'None'
        except:
            return 'lookup  error! '

    class Meta:
        model = Guowangbuliang
        fields = '__all__'


class GuowangbuliangViewSet(CustomModelViewSet):
    # permission_classes = []
    values_queryset = Guowangbuliang.objects.all()
    serializer_class = GuowangbuliangModelSerializer
    filter_class = GuowangbuliangFilter


from rest_framework.viewsets import ModelViewSet
class GuowangbuliangImageViewSet(ModelViewSet):
    # permission_classes = []
    queryset = GuowangbuliangImage.objects.all()
    serializer_class = GuowangbuliangImageModelSerializer

    def perform_create(self, serializer):
        # return super().perform_create(serializer)
        # image是前端传过来的图片，file_path是前端传过来的文件夹名
        image = serializer.validated_data['file_path']

        # print(self.request.data)

        # print('serializer.validated_data', serializer.validated_data)

        # file是保存进入的文件夹，计划为station，file_name是前端传过来的文件夹名，通过row可以找到对应的station
        file = self.request.data.get('station')
        # 取出来的id是字符串，需要转换成int
        try:
            # 如果int转换出错，就是None
            guowangbuliang_id = int(self.request.data.get('guowangbuliang_id'))
        except:
            guowangbuliang_id = None

        print('serializer.validated_data', serializer.validated_data)
        print(image, file, guowangbuliang_id)
        img_name, img_backend_relative_path = img_proccess_save(image, file, imageModel=serializer.Meta.model)

        guowangbuliang = Guowangbuliang.objects.get(id=guowangbuliang_id)

        print(guowangbuliang, img_name, img_backend_relative_path)

        serializer.save(file_path=img_backend_relative_path, guowangbuliang=guowangbuliang, file_name=img_name)

    def perform_destroy(self, instance):
        # return super().perform_destroy(instance)
        # print('instance', instance)
        img_proccess_delete(self, instance)
        instance.delete()


